import axios from 'axios'

const user = {
  state: {
    user: {
      id: 0,
      username: '',
      avatar: '',
      permissions: [],
    },
    tabs:[]
  },
  getters: {
    isWho: state => state.user.id > 0,
    username: state => state.user.username,
    avatar: state => state.user.avatar,
    permissions: state => state.user.permissions
  },

  mutations: {
    setUser(state, v) {
      state.user = v
    },
    setTabs(state, data) {
      state.tabs.push(data)
    }
  },
  actions: {
    getWho({ commit }) {
      return axios.get('/who').then(({data}) => {
        if (!data.errcode) {
          commit('setUser', data.data);
          return true;
        }
      });
    }
  }
}

export default user;
